package com.big.es.controller;

import com.big.common.result.Result;
import com.big.es.service.ProductService;
import com.big.model.model.es.MerchandiseEsVo;
import com.big.model.model.merchandise.MerchandiseDetail;
import com.big.model.vo.merchandise.MerchanParams;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.PrefixQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/api/es")
public class ProductController {


    @Autowired
    RestHighLevelClient client;

    @Autowired
    ProductService productService;

    @PostMapping("/searchLike/{search}")
    @ApiOperation(value="模糊查询")
    public Result SearchLike(@PathVariable  String search) throws IOException {
        /**
         * 没有的情况下进行子库搜搜到商品名称和简介
         */
        System.out.println(search);
        List<MerchandiseEsVo> merchandiseEsVos = productService.keyInfos(search);
        return  Result.ok(merchandiseEsVos);
    }

    @PostMapping("/searchSubmit/{page}/{limit}")
    @ApiOperation(value="模糊查询")
    public Result SearchLike(@PathVariable  Long  page, @PathVariable Long limit, MerchanParams merchanParams) throws IOException {
        System.out.println("值"+merchanParams);
        Map<String,Object>records=new HashMap<>();
        List<MerchandiseDetail> merchandiseDetails = productService.MerchandiseDetailESInfos(page, limit, merchanParams);
        records.put("records",merchandiseDetails);
        records.put("total",merchandiseDetails.size());
        records.put("size",limit);
        records.put("current",page);
        System.out.println(merchandiseDetails.size());
        return  Result.ok(records);

    }







}
